// pages/confirm/chosentime/chosentime.js
var app = getApp()
var time = require("../../../utils/util.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    step: 1,
    dateList: [],
    currentYear: new Date().getFullYear(),
    currentMonth: new Date().getMonth() + 1,
    currentDate: new Date().getDate(),
    currentTime: '',
    timeList: [{
        hour: "08",
        minutes: "00"
      },
      {
        hour: "09",
        minutes: "00"
      },
      {
        hour: "10",
        minutes: "00"
      },
      {
        hour: "11",
        minutes: "00"
      },
      {
        hour: "12",
        minutes: "00"
      },
      {
        hour: "13",
        minutes: "00"
      },
      {
        hour: "14",
        minutes: "00"
      },
      {
        hour: "15",
        minutes: "00"
      },
      {
        hour: "16",
        minutes: "00"
      },
      {
        hour: "17",
        minutes: "00"
      },
      {
        hour: "18",
        minutes: "00"
      },
      {
        hour: "19",
        minutes: "00"
      },
      {
        hour: "20",
        minutes: "00"
      }
    ],
    timeList1: [{
        hour: "08",
        minutes: "00"
      },
      {
        hour: "09",
        minutes: "00"
      },
      {
        hour: "10",
        minutes: "00"
      },
      {
        hour: "11",
        minutes: "00"
      },
      {
        hour: "12",
        minutes: "00"
      },
      {
        hour: "13",
        minutes: "00"
      },
      {
        hour: "14",
        minutes: "00"
      },
      {
        hour: "15",
        minutes: "00"
      },
      {
        hour: "16",
        minutes: "00"
      },
      {
        hour: "17",
        minutes: "00"
      },
      {
        hour: "18",
        minutes: "00"
      },
      {
        hour: "19",
        minutes: "00"
      },
      {
        hour: "20",
        minutes: "00"
      }
    ],
    dateActive: 0,
    timeActive: -10,
    info: {

    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      info: JSON.parse(options.info)
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.init(7,new Date().getMonth()+1+'-'+new Date().getDate())
  },

  init: function(num,date) {
    let that = this,
      dd = new Date(),
      today = new Date(),
      timeList = that.data.timeList,
      arr = [],
      time = [],
      targetday_milliseconds, i, j

    wx.showLoading({
      title: app.globalData.loadText,
      mask: true
    })

    // for (i = 0; i < num;i++) {
    //   dd.setDate(dd.getDate()+1)
    //   arr.push(dd.getMonth()+1+'/'+dd.getDate())
    // }

    // if (date == new Date().getDate()) {
    //   for (j in timeList) {
    //     time.push(timeList[j])
    //   }
    //   that.setData({
    //     timeList: time,
    //   })
    // } else {
    //   that.setData({
    //     timeList: that.data.timeList1,
    //   })
    // }

    // this.setData({
    //   dateList: arr
    // },() => {
    //   wx.hideLoading()
    // })

    for (i = 0; i <= num; i++) {
      targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * i
      today.setTime(targetday_milliseconds)
      if (i != 0) {
        dd.setDate(dd.getDate() + 1)
      }
      arr.push(dd.getMonth()+1 + '-' + dd.getDate())
    }

    if (date.split('-')[1] == new Date().getDate()) {
      for (j in timeList) {
        if (timeList[j].hour > new Date().getHours()) {
          time.push(timeList[j])
        }
      }
      that.setData({
        timeList: time,
      })
    } else {
      that.setData({
        timeList: that.data.timeList1,
      })
    }

    that.setData({
      dateList: arr
    },() => {
      wx.hideLoading()
    })
  },

  /**
   * 改变服务数量
   */
  changeStep: function(e) {
    this.setData({
      step: e.detail,
      'info.time.hours': e.detail
    })
  },

  /**
   * 选择时间（天）
   */
  selDay: function(e) {
    let that = this,
        date = e.currentTarget.dataset.day,
        timeList = that.data.timeList,
        k
    wx.showLoading({
      title: app.globalData.loadText,
      mask: true
    })
    that.setData({
      step: 1,
      dateActive: e.currentTarget.dataset.index,
      currentMonth: date.split('-')[0],
      currentDate: date.split('-')[1]
    })
    this.init(7, e.currentTarget.dataset.day)
  },

  /**
   * 选择时间（时 分）
   */
  selTime: function(e) {
    let that = this
    //     showTimeList = that.data.showTimeList,
    //     currentStart_at, currentEnd_at
    // currentStart_at = showTimeList[0][0].start
    // currentEnd_at = showTimeList[0][0].end
    // that.setData({
    //   timeActive: e.currentTarget.dataset.index,
    //   currentStart_at: currentStart_at,
    //   currentEnd_at: currentEnd_at
    // })
    that.setData({
      timeActive: e.currentTarget.dataset.index,
      currentTime: that.data.timeList[e.currentTarget.dataset.index]
    })
  },

  doHandleMonth: function(month) {
    var m = month
    if (month.toString().length == 1) {
      m = "0" + month
    }
    return m
  },

  /**
   * 取消时间选择
   */
  cancel: function() {
    wx.navigateBack()
  },

  /**
   * 确定时间
   */
  cfmDate: function() {
    let that = this,
      myApp = that.data,
      startAt, endAt
    if (myApp.currentTime == '') {
      wx.showToast({
        icon: 'none',
        title: '请选择服务时间',
      })
      return false
    }
    startAt = Date.parse(new Date(myApp.currentYear + '/' + myApp.currentMonth + '/' + myApp.currentDate + ' ' + myApp.currentTime.hour + ':' + myApp.currentTime.minutes))
    endAt = startAt + (myApp.info.time.hours * myApp.info.product.time * 60 * 1000)
    this.setData({
      'info.time.start_at': myApp.currentYear + '/' + myApp.currentMonth + '/' + myApp.currentDate + ' ' + myApp.currentTime.hour + ':' + myApp.currentTime.minutes,
      'info.time.end_at': time.formatTime(new Date(endAt))
    })
    wx.reLaunch({
      url: '/pages/confirm/confirm?info=' + JSON.stringify(myApp.info),
    })
    // wx.redirectTo({
    //   url: '../confirm?info=' + JSON.stringify(myApp.info),
    // })
  },
})